package com.app.controller;

import com.app.common.ConstantTool;
import com.app.model.FileModel;
import com.app.model.IncubatorsModel;
import com.app.service.FileService;
import com.app.service.IncubatorsService;
import com.app.util.Result;
import com.app.vo.EducationVo;
import com.app.vo.IncubatorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//孵化场地
@RestController
@Controller
@RequestMapping("/jiuquan/incubators")
public class IncubatorsController {
    @Autowired
    private IncubatorsService incubatorsService;
    @Autowired
    private FileService fileService;
    //查询列表
    @GetMapping("/getIncubatorsList")
    public Result getIncubatorsList(@RequestParam(defaultValue = "0") Integer page,
                                    @RequestParam(defaultValue = "999999") Integer limit, String name, Double longitude, Double latitude, Integer area) {
        IncubatorsVo incubatorsList = incubatorsService.getIncubatorsList(page, limit, name, longitude, latitude,area);
        return Result.success(incubatorsList);
    }
    //详情
    @GetMapping("/getIncubatorsById")
    public Result getIncubatorsById(Integer id) {
        IncubatorsModel incubators = incubatorsService.getById(id);
        List<String> urlList = new ArrayList<>();
        List<String> videoList = new ArrayList<>();
        String fileIdArr = incubators.getFileId();
        String[] split = fileIdArr.split(",");
        for (String fileIds : split) {
            int fileId = Integer.parseInt(fileIds);
            FileModel file = fileService.getById(fileId);
            if (file.getFileType() == 1) {
                urlList.add(file.getUrl());
            }
            if (file.getFileType() == 2) {
                videoList.add(file.getUrl());
            }
        }
        //设置返回地区的文字
        List<EducationVo> areaVoList = ConstantTool.AREA_VO_LIST;
        for (EducationVo education : areaVoList) {
            Integer areaId = education.getId();
            if (incubators.getArea() == areaId) {
                incubators.setAreaText(education.getData());
            }
        }
        incubators.setUrlList(urlList);
        return Result.success(incubators);
    }
    //用户场地详情
    @GetMapping("/getUserIncubatorsById")
    public Result getUserIncubatorsById(Integer id, Double longitude, Double latitude) {
        IncubatorsModel incubators = incubatorsService.getUserIncubatorsById(id, longitude, latitude);
        List<String> urlList = new ArrayList<>();
        String fileIdArr = incubators.getFileId();
        String[] split = fileIdArr.split(",");
        for (String fileIds : split) {
            int fileId = Integer.parseInt(fileIds);
            FileModel file = fileService.getById(fileId);
            if (file.getFileType() == 1) {
                urlList.add(file.getUrl());
            }
        }
        incubators.setUrlList(urlList);

        return Result.success(incubators);
    }
    //新增和修改
    @PostMapping("/addAndUpdateIncubators")
    public Result addAndUpdateIncubators(@RequestBody IncubatorsModel incubatorsModel) {
        IncubatorsModel incubators = incubatorsService.getById(incubatorsModel.getId());
        if (incubators != null) {
            incubatorsService.updateIncubators(incubatorsModel);
            return Result.success("修改成功");
        } else {
            incubatorsService.saveIncubators(incubatorsModel);
            return Result.success("添加成功");
        }
    }
    //删除
    @GetMapping("/delIncubators")
    public Result delIncubators(Integer id) {
        incubatorsService.removeById(id);
        return Result.success("删除成功");
    }
}
